package com.sky.service.impl;/*
@author sun
@date 24/07/15
@project IntelliJ IDEA
*/

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    // 新增套餐
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertOneSetmeal(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });


        setmealDishMapper.insertAllSetmeal(setmealDishes);
    }

    // 分页查询
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {

        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = (Page<SetmealVO>) setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //    套餐启、停售
    @Transactional
    public void startOrStopBySetmeal(Integer status, Long id) {

        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.DISABLE){
            List<Dish> dishList = dishMapper.getSetmealById(id);
            if (dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }

        }

        Setmeal setmeal = new Setmeal().builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    //    根据id查询套餐，用于修改页面回显数据
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;

    }

    //    TODO 批量删除套餐  (相关联的删两次) 用stream流效率高  7-23完成需求
    @Transactional
    public void delSetmeals(List<Long> ids) {

        // 循环遍历用stream流效率高些
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id); // 根据id查到套餐所有字段然后判断状态
            if (StatusConstant.ENABLE == setmeal.getStatus()){// 起售中套餐不能删除
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId -> {
            setmealMapper.delSetmeal(setmealId); // 根据主表主键id先删除
            setmealDishMapper.delSetmealWithSetmeal(setmealId); // 根据主表相关联的数据id再删除
        });
    }
}
